package polimi.sc.expoquest.storage.datasources;

import java.util.ArrayList;
import java.util.Collection;
import polimi.sc.expoquest.storage.DBContract;
import polimi.sc.expoquest.storage.DBHelper;
import polimi.sc.expoquest.storage.model.Tag;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

/**
 * This class is in charge of managing all the database
 * operations related to the TAG TABLE, using the 
 * database helper, which does the direct calls to the 
 * database
 * @author Monica Lozano
 *
 */
public class TagDataSource {
	//------------------
	//ATTRIBUTES
	//------------------
	public static final int NO_ID_FOR_TAG =-1;
	/**
	 * The object that represents the database
	 */
	private SQLiteDatabase db;
	/**
	 * The database helper for accessing the database 
	 */
	private DBHelper dbHelper;
	
	//----------------
	// CONSTRUCTOR
	//----------------
	public TagDataSource(Context ctx)
	{
		//1. We create the DB helper
		dbHelper = new DBHelper(ctx);
	}
	
	//------------------
	//METHODS
	//------------------
	//utilities
	
	/**
	 * Creates the Tag object from the data in the current
	 * row of the cursor.
	 * @param cursor
	 * @return
	 */
	private Tag cursorToTag(Cursor cursor)
	{
		Tag t = new Tag();
		//1. We obtain the columns indexes based on their names
		int idColIndex = cursor.getColumnIndex(DBContract.Tag._ID);
		int tagColIndex = cursor.getColumnIndex(DBContract.Tag.CLM_NAME_TEXT);
		//2.We obtain the data from the row based on the index
		int id =  cursor.getInt(idColIndex);
		String text = cursor.getString(tagColIndex);
		//3.We save the data in the object
		t.setId(id);
		t.setTag(text);
		return t;
	}
	
	
	//database connection
	/**
	 * Opens the database such that we can used it for reading and writing
	 */
	public void open(){
		/* 
		 * TODO put this call in a asynchronous task, such that if it takes more than
		 * few seconds our application wont colapse.
		 */
		dbHelper.open();
		db = dbHelper.getWritableDatabase();
	}
	/**
	 * Close the database connection
	 */
	public void close()
	{
		dbHelper.close();
	}
	
	//Database operations
	/**
	 * @param tag: The tag for which we are looking for the ID
	 * @return Returns the id of the tag given
	 */
	public int getTagID(String tag)
	{
		//3. We return the alarm object saved succesfully into the database
		Cursor cursor = db.query(DBContract.Tag.TABLE_NAME, 
								DBContract.Tag.ALL_COLUMNS, 
								DBContract.Tag.CLM_NAME_TEXT+ "='"+ tag+"'", 
								null, null, null, null);
		//3.1. Move the cursor to recent inserted row
		boolean notEmpty =cursor.moveToFirst();
		Tag t = null;
		if(notEmpty){
			//3.2. Creating the Alarm object
			t = cursorToTag(cursor);
			//3.3 Closing the cursor
			cursor.close();
		}
		int id = t==null? NO_ID_FOR_TAG: t.getId();
		return id;
	}
	
	
	/**
	 * Returns all the tags available
	 * @return
	 */
	public Collection<Tag> getAllTags()
	{
		Collection<Tag> tags = new ArrayList<Tag>();
		//1. Doing the consult
		Cursor cursor = db.query(DBContract.Tag.TABLE_NAME,DBContract.Tag.ALL_COLUMNS,
								null,null,null,null,null);
		//2. We create the list from the results of the query
		cursor.moveToFirst();
		while(!cursor.isAfterLast())
		{
			Tag t = cursorToTag(cursor);
			tags.add(t);
			cursor.moveToNext();
		}
		cursor.close();
		return tags;
	}
	
	/**
	 * Inserts a tag
	 * @param tag
	 * @return
	 */
	public Tag insert(String tag)
	{
		//1.We create the values for saving them into the database
		ContentValues values = generateValues(tag);
		//2. We save it on the database
		long insertID= db.insert(DBContract.Tag.TABLE_NAME, null, values);
		//3. We return the alarm object saved succesfully into the database
		Cursor cursor = db.query(DBContract.Tag.TABLE_NAME, 
								DBContract.Tag.ALL_COLUMNS, 
								DBContract.Tag._ID+ "="+ insertID, 
								null, null, null, null);
		//3.1. Move the cursor to recent inserted row
		cursor.moveToFirst();
		//3.2. Creating the Alarm object
		Tag t = cursorToTag(cursor);
		//3.3 Closing the cursor
		cursor.close();
		return t;
	}
	/**
	 * Create the values for query
	 * @param tag
	 * @return
	 */
	private ContentValues generateValues(String tag)
	{
		ContentValues values = new ContentValues();
		values.put(DBContract.Tag.CLM_NAME_TEXT, tag);
		return values;
	}
	/**
	 * Deletes a tag
	 * @param t
	 * @return
	 */
	public boolean delete(Tag t)
	{
		long id = t.getId();
		int total= db.delete(DBContract.Tag.TABLE_NAME,
				  DBContract.Tag._ID+"="+id, null);
		return total>0;
	}
}
